package com.tencent.iov.cowin.befsale.clue.controller.clue.assembler;

import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddCustomerFollowRecordRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateClueEmpRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateCustomerRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerFollowPlanResponse;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueCustomerCollect;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueFail;
import com.tencent.iov.cowin.befsale.clue.value.enums.ClueStatsEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.FailStatusEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.FollowContentEnum;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.BaseAssembler;
import com.software.boot.common.model.rest.BaseRequest;
import com.software.boot.common.model.rest.BaseResponse;
import com.software.boot.common.util.EmptyUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.DeleteEnum;
import com.software.common.enums.SCRMRequestHeaderEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @Author: whp
 * @Date: 2021-12-23 14:53:43
 * @Description: 线索客户宽表适配器，负责vo<->entity、vo<->po的转换和必要的参数检查，行为
 */
@Slf4j
public class ClueCustomerCollectAssembler extends BaseAssembler<BaseResponse, BaseRequest, ClueCustomerCollect> {

    public static void initAddClueCustomerCollect(ClueCustomerCollect request) {
        if (EmptyUtils.isEmpty(request.getCorpId())) {
            request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getTenantId())) {
            request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedBy())) {
            request.setCreatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedBy())) {
            request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedName())) {
            request.setCreatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedName())) {
            request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getDealerId())) {
            request.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedTime())) {
            request.setCreatedTime(new Date());
        }
        if (EmptyUtils.isEmpty(request.getUpdatedTime())) {
            request.setUpdatedTime(new Date());
        }
        request.setDeleted(DeleteEnum.INIT.getFlag());
    }

    public static ClueCustomerCollect initClueCustomerCollect(ClueCustomerCollect request) {
        request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        request.setCreatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        request.setCreatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    public static ClueCustomerCollect initUpdateClueCustomerCollect(ClueCustomerCollect request) {
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    /**
     * 分配人员更新块表数据初始化
     *
     * @param request 参数
     * @return 宽表信息
     */
    public static ClueCustomerCollect initUpdateByDistributeInfo(UpdateClueEmpRequest request) {
        ClueCustomerCollect collectRequest = new ClueCustomerCollect();
        collectRequest.setFollowEmpId(request.getFollowEmpId());
        collectRequest.setFollowEmpName(request.getFollowEmpName());
        collectRequest.setDistributeTime(new Date());
        return initUpdateClueCustomerCollect(collectRequest);
    }

    /**
     * 战败审批更新块表数据初始化
     *
     * @param request 参数
     * @return 宽表信息
     */
    public static ClueCustomerCollect initUpdateByFailInfo(UpdateClueEmpRequest request, CustomerFollowPlanResponse followPlanResponse, ClueFail clueFail) {
        ClueCustomerCollect collectRequest = new ClueCustomerCollect();
        FailStatusEnum failStatusEnum = FailStatusEnum.getByCode(request.getFailStatus());
        if (EmptyUtils.isEmpty(failStatusEnum)) {
            throw new BusinessException(NEError.BUSINESS_ERROR, "线索战败枚举不存在");
        }
        switch (failStatusEnum) {
            case AGREE:
                //同意战败-更新计划跟进记录全部为null
                collectRequest.setPlanFollowTime(null);
                collectRequest.setPlanFollowId(null);
                collectRequest.setPlanFollowTimeCode(null);
                collectRequest.setPlanFollowContent(null);
                collectRequest.setClueStatus(ClueStatsEnum.IS_FAIL.getCode());
                collectRequest.setFailStatus(FailStatusEnum.AGREE.getCode());
                collectRequest.setApproveCreatedTime(new Date());
                break;
            case ACTIVATE:
                collectRequest.setFollowEmpId(request.getFollowEmpId());
                collectRequest.setFollowEmpName(request.getFollowEmpName());
                collectRequest.setDistributeTime(new Date());
                collectRequest.setClueStatus(clueFail.getClueStatus());
                collectRequest.setApproveRemark(request.getApproveRemark());
                collectRequest.setFailStatus(FailStatusEnum.ACTIVATE.getCode());
                collectRequest.setPlanFollowTime(followPlanResponse.getPlanFollowTime());
                collectRequest.setPlanFollowId(followPlanResponse.getId());
                collectRequest.setPlanFollowContent(followPlanResponse.getPlanFollowContent());
                break;
            case REFUSE:
                collectRequest.setFollowEmpId(request.getFollowEmpId());
                collectRequest.setFollowEmpName(request.getFollowEmpName());
                collectRequest.setDistributeTime(new Date());
                collectRequest.setClueStatus(clueFail.getClueStatus());
                collectRequest.setApproveRemark(request.getApproveRemark());
                collectRequest.setFailStatus(FailStatusEnum.REFUSE.getCode());
                collectRequest.setApproveCreatedTime(new Date());
            default:
                break;
        }
        return collectRequest;
    }

    /**
     * 根据跟进信息填充宽表信息
     *
     * @param request      参数
     * @param followRecord 跟进信息
     * @return 宽表信息
     */
    public static ClueCustomerCollect initUpdateByFollowInfo(ClueCustomerCollect request, AddCustomerFollowRecordRequest followRecord, ClueCustomerCollect exist) {
        request.setFollowRemark(followRecord.getFollowRemark());
        request.setFollowTime(new Date());
        request.setProdCode(followRecord.getProdCode());
        request.setFollowContent(followRecord.getFollowContent());

        //填充跟进状态
        request.setClueId(followRecord.getClueId());
        if (EmptyUtils.isEmpty(followRecord.getFollowContent())) {
            return initUpdateClueCustomerCollect(request);
        }
        FollowContentEnum followContentEnum = FollowContentEnum.getByCode(Integer.parseInt(followRecord.getFollowContent()));
        if (EmptyUtils.isEmpty(followContentEnum)) {
            return null;
        }
        switch (followContentEnum) {
            case PRODUCT_INTRO:
                if (exist.getStatus() == null || exist.getStatus().equals(ClueStatsEnum.FOLLOW_UP.getCode())) {
                    request.setStatus(ClueStatsEnum.FOLLOW_UP.getCode());
                }
                break;
            case CHECKIN:
                request.setClueStatus(ClueStatsEnum.CHECKIN.getCode());
                break;
            case DRIVE_TEST:
                //试乘试驾时间取当前时间
                request.setDriveTime(new Date());
//                request.setDriveTime(EmptyUtils.isEmpty(followRecord.getOperateTime()) ? new Date() : followRecord.getOperateTime());
                request.setClueStatus(ClueStatsEnum.CHECKIN.getCode());
                break;
            case ORDER:
                request.setOrderCreatedTime(EmptyUtils.isEmpty(followRecord.getOperateTime()) ? new Date() : followRecord.getOperateTime());
                request.setClueStatus(ClueStatsEnum.ORDER.getCode());
                break;
            case DELIVERY:
                request.setClueStatus(ClueStatsEnum.DELIVERY.getCode());
                request.setPlanFollowId(null);
                request.setPlanFollowContent(null);
                request.setPlanFollowTime(null);
                request.setPlanFollowTimeCode(null);
                break;
            default:
                break;
        }
        return initUpdateClueCustomerCollect(request);
    }

    public static ClueCustomerCollect initUpdateByCustomerInfo(UpdateCustomerRequest updateCustomer) {
        ClueCustomerCollect clueCustomerCollect = new ClueCustomerCollect();
        clueCustomerCollect.setName(updateCustomer.getName());
        clueCustomerCollect.setPhone(updateCustomer.getPhone());
        clueCustomerCollect.setPhoneBak(updateCustomer.getPhoneBak());
        clueCustomerCollect.setCodeNameInfo(updateCustomer.getCodeNameInfo());
        clueCustomerCollect.setAvatar(updateCustomer.getAvatar());
        clueCustomerCollect.setGender(updateCustomer.getGender());
        clueCustomerCollect.setTypeCode(updateCustomer.getTypeCode());
        clueCustomerCollect.setAge(updateCustomer.getAge());
        clueCustomerCollect.setQwName(updateCustomer.getQwName());
        clueCustomerCollect.setBirthday(updateCustomer.getBirthday());
        clueCustomerCollect.setProvinceCode(updateCustomer.getProvinceCode());
        clueCustomerCollect.setCityCode(updateCustomer.getCityCode());
        clueCustomerCollect.setDistrictCode(updateCustomer.getDistrictCode());
        clueCustomerCollect.setAddress(updateCustomer.getAddress());
        clueCustomerCollect.setCusRemark(updateCustomer.getCusRemark());
        clueCustomerCollect.setMarriage(updateCustomer.getMarriage());
        return clueCustomerCollect;
    }
}
